package com.project.fortuneteller.exception.hander;



import com.project.fortuneteller.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.security.SignatureException;
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {SignatureException.class})
    @ResponseBody
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result exceptionHandler(SignatureException e){
       log.info("未登录");
        //这里先判断拦截到的Exception是不是我们自定义的异常类型
        if(e!=null){
            return Result.error("token错误，请重新登录");
        }

        return null;
    }

    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result exceptionHandler(Exception e){
         log.info("服务器内部异常");
        if(e != null){
            return Result.error( "后端异常，请联系数据库管理员");
        }
       return null;
    }

}
